#include <iostream>
//
// Created by 86131 on 2023/10/24.
//

#include <iostream>
using namespace std;
template <typename T>
class ArrayList {
private:
    T* data;
    int size;
    int capacity;

public:
    ArrayList() : data(nullptr), size(0), capacity(0) {}

    ~ArrayList() {
        delete[] data;
    }

    // 添加元素
    void add(const T& item) {
        if (size == capacity) {
            // 扩展容量
            int newCapacity = capacity == 0 ? 1 : capacity * 2;
            T* newData = new T[newCapacity];
            for (int i = 0; i < size; i++) {
                newData[i] = data[i];
            }
            delete[] data;
            data = newData;
            capacity = newCapacity;
        }
        data[size] = item;
        size++;
    }

    // 获取指定位置的元素
    T& get(int index) {
        if (index >= 0 && index < size) {
            return data[index];
        } else {
            std::cout << "索引越界。" << std::endl;
            // 返回一个默认值，具体处理方式可以根据需求进行修改
            return data[0];
        }
    }

    // 更新指定位置的元素
    void set(int index, const T& item) {
        if (index >= 0 && index < size) {
            data[index] = item;
        } else {
            std::cout << "索引越界。" << std::endl;
        }
    }

    // 删除指定位置的元素
    void remove(int index) {
        if (index >= 0 && index < size) {
            for (int i = index; i < size - 1; i++) {
                data[i] = data[i + 1];
            }
            size--;
        } else {
            std::cout << "索引越界。" << std::endl;
        }
    }

    // 获取线性表的大小
    int getSize() const {
        return size;
    }
    //改变线性表大小
    void setSize(int size1){
        this->size = size1;
    }
    bool empty(){
        return this->size==0;
    }
    int end(){
        return data[size-1];
    }
    int index(int value){
        for (int i = 0; i < size - 1; i++) {
            if(value ==data[i]){return i;}
        }
        cout<<"未找到";
        return 0;
    }
};

using namespace std;
template<typename T>
class queue {
private:
    ArrayList<T> list;

public:
    // 入队
    void push(const T& item) {
        list.add(item);
    }

    // 出队
    void pop() {
        if (!list.empty()) {
            list.remove(0);
        } else {
            std::cout << "队列为空，无法出队。" << std::endl;
        }
    }

    // 获取队头元素
    T& front() {
        return list.get(0);
    }

    // 判断队列是否为空
    bool empty() const {
        return list.getSize() == 0;
    }

    // 获取队列的大小
    int getSize() const {
        return list.getSize();
    }
};

const int N = 1010;
int in_order[N], post_order[N];
int n;

struct node {
    int data;
    node *lchild;
    node *rchild;
};

node *build(int inL, int inR, int postL, int postR) {
    if (inL > inR)
        return NULL;
    node *root;
    root = new node;
    root->data = post_order[postL];
    int k = inL;
    while (in_order[k] != root->data)
        k++;
    int numLeft = k - inL;
    root->lchild = build(inL, k - 1, postL+1, postL + numLeft);
    root->rchild = build(k + 1, inR, postL + numLeft+1, postR);
    return root;
}

void bfs(node *root) {
    int num = 0;
    queue<node *>q;
    q.push(root);
    while (q.getSize()) {
        node *now = q.front();
        q.pop();
        cout << now->data;
        num++;
        if (num < n)
            cout << " ";
        if (now->rchild != NULL)
            q.push(now->rchild);
        if (now->lchild != NULL)
            q.push(now->lchild);
    }
}


int main() {
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> in_order[i];
    for (int i = 0; i < n; i++)
        cin >> post_order[i];
    node *root = build(0, n - 1, 0, n - 1);
    bfs(root);
    return 0;
}